package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: itcast_health
 * @create: 2019-10-18 15:42
 * @author: li he
 * @description:
 */
@RestController
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;


    @RequestMapping("add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.add(checkItem);
            long total = checkItemService.findTotal();
            return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS,total);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("findPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return checkItemService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(".checkItem.findPage-----异常");
            return new PageResult(0L,null);

        }
    }

    @RequestMapping("delById")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delById(int id){
        try {
            checkItemService.delById(id);
            long total = checkItemService.findTotal();
            return new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS,total);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_CHECKITEM_SUCCESS);
        }
    }

    @RequestMapping("findById")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findById(int id){
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
            return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("findAll")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll(){
        try {
            List<CheckItem> checkItemList =  checkItemService.findAll();
            return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.QUERY_CHECKITEM_FAIL);
        }
    }

}